package com.xja.controller;/**
 * @className: UserController
 * @description: TODO 类描述
 * @author: wkx
 * @date: 2022/4/17
 **/

import com.xja.pojo.BlogUsers;
import com.xja.pojo.FocusInfo;
import com.xja.service.ArticleService;
import com.xja.service.UserService;
import com.xja.util.FileUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : UserController  //类名

 * @Author : Lenovo //作者
 * @Date: 2022/4/17  10:56
 */
@Controller
@RequestMapping("user")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @Reference(url = "dubbo://localhost:20881")
    private UserService userService;
    @Reference(url = "dubbo://localhost:20883")
    private ArticleService articleService;

    /*
    *@作者:褚
    *@方法:普通用户登录的方法
    *@创建时间:2022/4/17 15:19
    **/
    @RequestMapping("userLogin")
    private String userLogin(HttpSession session, BlogUsers blogUsers){
        logger.info("普通用户登录的方法");
        BlogUsers user = userService.login(blogUsers);
        session.setAttribute("user",user);
        return "redirect:/blog/getIndex";
    }
    /*
    *@作者:褚
    *@方法:退出登录
    *@创建时间:2022/4/21 21:02
    **/
    @RequestMapping("loginOut")
    private String loginOut(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/login.html";
    }
    /*
    *@作者:褚
    *@方法:普通用户注册的方法
    *@创建时间:2022/4/17 17:19
    **/
    @RequestMapping("userRegister")
    private String userRegister(BlogUsers blogUsers, MultipartFile file){
        logger.info("普通用户注册的方法");
        String path = null;
        try {
            path = FileUtil.getTransferToPath(file);
            blogUsers.setHeadimg(path);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        userService.register(blogUsers);
        return "redirect:/login.html";
    }
    /*
    *@作者:褚
    *@方法:关注的博主的信息
    *@创建时间:2022/4/18 16:18
    **/
    @RequestMapping("focus")
    private String focus(HttpSession session, Model model){
        BlogUsers user = (BlogUsers) session.getAttribute("user");
        List<FocusInfo> targetList = userService.selectFocusByUid(user.getUid());//关注信息
        Map<Integer,Object> userListMap = new HashMap<>();//关注的用户信息
        Map<Integer, Integer> articleCountMap = new HashMap<>();//关注的用户发表的文章数
        HashMap<Integer, Integer> fansMap = new HashMap<>();//关注的用户的粉丝数
        for (FocusInfo info:targetList){
            BlogUsers blogUsers = userService.selectUserById(info.getTargetUid());
            userListMap.put(info.getTargetUid(),blogUsers);
            Integer focusCount = articleService.selectArticleCount(info.getTargetUid());
            articleCountMap.put(info.getTargetUid(),focusCount);
            Integer fansCount = userService.selectSourceCountByUid(info.getTargetUid());
            fansMap.put(info.getTargetUid(),fansCount);
        }
        model.addAttribute("targetList",targetList);
        model.addAttribute("userListMap",userListMap);
        model.addAttribute("articleCountMap",articleCountMap);
        model.addAttribute("fansMap",fansMap);
        return "focus";
    }
    /*
    *@作者:褚
    *@方法:粉丝的信息
    *@创建时间:2022/4/18 16:19
    **/
    @RequestMapping("fans")
    private String fans(HttpSession session,Model model){
        BlogUsers user = (BlogUsers) session.getAttribute("user");
        List<FocusInfo> sourceList = userService.selectSourceById(user.getUid());//粉丝的用户信息
        Map<Integer,Object> userListMap = new HashMap<>();//关注的用户信息
        Map<Integer, Integer> articleCountMap = new HashMap<>();//粉丝发表的文章数
        HashMap<Integer, Integer> fansMap = new HashMap<>();//粉丝的粉丝数
        for (FocusInfo info:sourceList){
            BlogUsers blogUsers = userService.selectUserById(info.getScourceUid());
            userListMap.put(info.getScourceUid(),blogUsers);
            Integer sourceCount = articleService.selectArticleCount(info.getScourceUid());
            articleCountMap.put(info.getScourceUid(),sourceCount);
            Integer fansCount = userService.selectSourceCountByUid(info.getScourceUid());
            fansMap.put(info.getScourceUid(),fansCount);
        }
        model.addAttribute("sourceList",sourceList);
        model.addAttribute("userListMap",userListMap);
        model.addAttribute("articleCountMap",articleCountMap);
        model.addAttribute("fansMap",fansMap);
        return "fans";
    }
    /*
    *@作者:褚
    *@方法:取消关注
    *@创建时间:2022/4/18 19:13
    **/
    @RequestMapping("getCancel")
    @ResponseBody
    private Boolean getCancel( HttpSession session,Integer targetUid){
        logger.info("取消关注");
        BlogUsers user = (BlogUsers) session.getAttribute("user");
        Integer sourceUid = user.getUid();
        boolean flag = userService.deleteFocusInfo(targetUid,sourceUid);
        return flag;
    }
}
